---
--类名: FadeWordsView.lua
--创建日期: 2016-8-29
--创建人: 陈小虎
--功能描述: 屏幕渐变字
--修改内容:
--修改日期:   
--修改人:     
--修改备注: 
--

local FadeWordsView = class("FadeWordsView",function()
    return display.newLayer("FadeWordsView")
end)

function FadeWordsView:ctor(params)
	self.mWords = params.words
	self.mFontSize = params.fontSize
	self.mDelayTime = params.delayTime
	self.mFadeTime = params.fadeTime
	self.mStayTime = params.stayTime or 0
	self.mLineWidth = params.lineWidth
	self.mOverDelayTime = params.overDelayTime
	self.mColor = params.color
    self.mSy = params.sy
    self.mNextTime = params.nextTime
	self.mCallback = params.callback
    self.mCallback1 = params.callback1
    self.mShowType = params.showType or 1 -- 默认为空表示 按行显示

    local strArray = string.split(self.mWords,"|")
    local strLen = #strArray
    local str = ""
    local layouts = {}
    for i,v in ipairs(strArray) do

        if i ~= 1 then 
            str = str .. "\n"
        end
        -- 查询当前行的对齐方式
        local arr1 = string.split(v, "<Left>")
        local arr2 = string.split(v, "<Right>")
        if #arr1 == 2 then
            layouts[i] = "Left"
            str = str .. arr1[2] -- 默认关键字在每行的开头
        elseif #arr2 == 2 then
            layouts[i] = "Right"
            str = str .. arr2[2] -- 默认关键字在每行的开头
        else
            -- layouts[i] = "Mid"
            str = str .. v
        end
    end

	self.mWordsTab = string.utable(str) or {}

	self.ttfConfig = {}
    self.ttfConfig.fontFilePath = Res.font
    self.ttfConfig.fontSize = self.mFontSize

    self.mLines = {}

    local width = 0
    local height = 0
    local linePointer = 1
    for i,v in ipairs(self.mWordsTab) do
    	local word = v
    	local wordLabel = cc.Label:createWithTTF(self.ttfConfig,word, cc.TEXT_ALIGNMENT_LEFT)
    	width = width + wordLabel:getBoundingBox().width
    	if height == 0 then
    		height = wordLabel:getBoundingBox().height
    	end
    	if word == "\n" or width > self.mLineWidth then
    		linePointer = linePointer + 1
    		width = 0
    	end
    	if self.mLines[linePointer] == nil then
    		self.mLines[linePointer] = {}
    	end

        if word ~= "\n" then
            table.insert(self.mLines[linePointer],word)
        end
    end

    local function toString(tb)
    	local str = ""
    	for i,v in ipairs(tb) do
    		str = str .. v
    	end
    	return str
    end

    local lines = #self.mLines
    if lines == 0 then
    	self:callback()
    	return
    end
    local totalHeight = lines * height

    local sx = display.safe_width / 2 + display.safe_x
    local sy = self.mSy or (display.height - totalHeight) / 2 + totalHeight

    local intervalTime = self.mFadeTime / lines
    local leftX, rightX 

    self.mLabels = {}

    local function fadeOutLabels()
        for i,v in ipairs(self.mLabels) do
            local action = cc.Sequence:create(cc.FadeOut:create(self.mOverDelayTime))
            v:runAction(action)
        end
    end

    for i,v in ipairs(self.mLines) do
    	local lineStr = toString(v)
    	local wordsLabel = cc.Label:createWithTTF(self.ttfConfig,lineStr, cc.TEXT_ALIGNMENT_LEFT)
    	wordsLabel:setTextColor(cc.c4b(self.mColor.r, self.mColor.g, self.mColor.b, self.mColor.a))
    	wordsLabel:setAnchorPoint(cc.p(0.5,1))
    	local y = sy - (i - 1) * height * 1.3 -- 1.3倍行距
    	wordsLabel:setPosition(cc.p(sx,y))
    	self:addChild(wordsLabel)
    	wordsLabel:setOpacity(0)
        table.insert(self.mLabels,wordsLabel)

        -- 当前行的对齐方式
        if i ~= 1 then
            if layouts[i] == "Left" then
                wordsLabel:setAnchorPoint(cc.p(0, 1))
                wordsLabel:setPositionX(leftX)
            elseif layouts[i] == "Right" then
                wordsLabel:setAnchorPoint(cc.p(1, 1))
                wordsLabel:setPositionX(rightX)
            end
        end

    	local delayTime = intervalTime * (i - 1) + self.mDelayTime
        if self.mShowType == 0 then -- 一起显示
            delayTime = self.mDelayTime
        end
    	local action 
    	if i == lines then
    		action = cc.Sequence:create(cc.DelayTime:create(delayTime),cc.FadeIn:create(intervalTime),cc.CallFunc:create(function()
    			local overDelayAction = cc.Sequence:create(cc.DelayTime:create(self.mStayTime),cc.CallFunc:create(function()
                    fadeOutLabels()
                end),cc.DelayTime:create(self.mOverDelayTime),
                cc.CallFunc:create(function()
    				self:callback1()
    			end))
    			self:runAction(overDelayAction)
    		end))
    	else
    		action = cc.Sequence:create(cc.DelayTime:create(delayTime),cc.FadeIn:create(intervalTime))
    	end
    	wordsLabel:runAction(action)

        -- 后面行的排列方式要以第一行的为标准
        if i == 1 then 
            local width = wordsLabel:getBoundingBox().width
            leftX = sx - width / 2
            rightX = sx + width / 2
        end
    end

    local action = cc.Sequence:create(cc.DelayTime:create(self.mNextTime),cc.CallFunc:create(function()
        self:callback()
    end))
    self:runAction(action)
end

function FadeWordsView:callback()
	if self.mCallback then
		self.mCallback()
	end
end

function FadeWordsView:callback1()
    if self.mCallback1 then
        self.mCallback1()
    end
end

return FadeWordsView